package com.book.lizhi.util;

import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;

import com.book.lizhi.AppApplication;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.model.BaseModel;

public class Util {
	public static void setIconEnable(Menu menu, boolean enable) {
		try {
			Class<?> clazz = Class.forName("android.support.v7.internal.view.menu.MenuBuilder");
			Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
			m.setAccessible(true);

			// MenuBuilder实现Menu接口，创建菜单时，传进来的menu其实就是MenuBuilder对象(java的多态特征)
			m.invoke(menu, enable);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static int getPackageVersion() {
		try {
			PackageInfo info = AppApplication.instance.getPackageManager().getPackageInfo(AppApplication.instance.getPackageName(), 0);
			return info.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static byte encryptMethod(int b) {
		// int mask = 0x01;
		// return (byte) (b = (byte) ((b & ~mask) | ((b & mask) ^ mask)));
		return (byte) b;
	}

	public static String percentFormate(double percent) {
		DecimalFormat format = new DecimalFormat("###.00%");
		return format.format(percent);
	}

	public static void sortByTime(List<? extends BaseModel> list) {
		Collections.sort(list, new Comparator<BaseModel>() {

			@Override
			public int compare(BaseModel lhs, BaseModel rhs) {
				return rhs.time.compareTo(lhs.time);
			}
		});
	}

	public static void saveDataBySharePreference(String key, int value) {
		SharedPreferences preferences = AppApplication.instance.getSharedPreferences(Constants.SHAREPREFERENCE_SETTING, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public static void saveDataBySharePreference(String key, String value) {
		SharedPreferences preferences = AppApplication.instance.getSharedPreferences(Constants.SHAREPREFERENCE_SETTING, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

	public static String getStringBySharePreference(String key) {
		SharedPreferences preferences = AppApplication.instance.getSharedPreferences(Constants.SHAREPREFERENCE_SETTING, Context.MODE_PRIVATE);
		return preferences.getString(key, null);
	}

	public static int getIntBySharePreference(String key) {
		SharedPreferences preferences = AppApplication.instance.getSharedPreferences(Constants.SHAREPREFERENCE_SETTING, Context.MODE_PRIVATE);
		return preferences.getInt(key, 0);
	}

	public static boolean getBooleanBySharePreference(String key) {
		SharedPreferences preferences = AppApplication.instance.getSharedPreferences(Constants.SHAREPREFERENCE_SETTING, Context.MODE_PRIVATE);
		return preferences.getBoolean(key, true);
	}

	public static boolean getAutoGetSharePreference(String key) {
		SharedPreferences preferences = AppApplication.instance.getSharedPreferences(Constants.SHAREPREFERENCE_SETTING, Context.MODE_PRIVATE);
		return preferences.getBoolean(key, true);
	}

	public static void saveDataBySharePreference(String key, boolean value) {
		SharedPreferences preferences = AppApplication.instance.getSharedPreferences(Constants.SHAREPREFERENCE_SETTING, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

}
